package extras.salvation.api.methods;

import org.rsbuddy.tabs.Inventory;

import com.rsbuddy.script.wrappers.Item;

/**
 * @author Salvation
 * @version 1.0
 * @since 20-08-2011
 */
public final class InventoryCache {

	private static Item[] CACHE = null;

	/**
	 * @author Salvation
	 * @description Updates the cached values.
	 * @see getCache()
	 */
	public static void update() {
		CACHE = Inventory.getAllItems();
	}

	/**
	 * @author Salvation
	 * @return The cached values.
	 * @see update()
	 */
	public static Item[] getCache() {
		return CACHE;
	}

	/**
	 * @author Salvation
	 * @param index The index of the Item in the Inventory. 0-27.
	 * @return The Item found at this index, if there no real Item found an empty Item will be returned. Where the id of this Item is -1.
	 */
	public static Item getItemAt(final int index) {
		return CACHE[index];
	}

	/**
	 * @author Salvation
	 * @param id The id of the Item to check.
	 * @return <tt>true</tt> if an Item with this id was found in the cache, otherwise <tt>false</tt>.
	 */
	public static boolean contains(final int id) {
		for (final Item i : CACHE) {
			if (i.getId() == id) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @author Salvation
	 * @param ids The ids of the Items to check for.
	 * @return <tt>true</tt> if an Item with either of the ids was found in the cache, otherwise <tt>false</tt>.
	 */
	public static boolean containsOneOf(final int...ids) {
		for (final Item i : CACHE) {
			if (ExtendedArrays.contains(ids, i.getId())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @author Salvation
	 * @param ids The ids of the Items to check for.
	 * @return <tt>true</tt> if all ids were found in the cache, otherwise <tt>false</tt>.
	 */
	public static boolean containsAllOf(final int...ids) {
		for (final int i : ids) {
			if (!contains(i)) return false;
		}
		return true;
	}

	/**
	 * @author Salvation
	 * @param includeStack <tt>true</tt> to count in stacks. For if you have 1337 coins this will return 1337 if <tt>true</tt> but will return 1 (because it takes up 1 slot) if <tt>false</tt>.
	 * @return The count of the amount of Items in the cache, matching your arguments.
	 */
	public static int getCount(final boolean includeStack) {
		int count = 0;
		for (final Item i : CACHE) {
			if (!i.getName().equals("")) {
				count += includeStack ? i.getStackSize() : 1;
			}
		}
		return count;
	}
	/**
	 * @author Salvation
	 * @param includeStack <tt>true</tt> to count in stacks. For if you have 1337 coins this will return 1337 if <tt>true</tt> but will return 1 (because it takes up 1 slot) if <tt>false</tt>.
	 * @param ids The ids to count.
	 * @return The count of the amount of Items in the cache, matching your arguments.
	 */
	public static int getCount(final boolean includeStack, final int...ids) {
		int count = 0;
		for (final Item i : CACHE) {
			if (ExtendedArrays.contains(ids, i.getId())) {
				count += includeStack ? i.getStackSize() : 1;
			}
		}
		return count;
	}
}
